package com.yzlwb.common.utils.http;

import com.aliyun.oss.common.comm.HttpDeleteWithBody;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * http请求工具类
 * @author wfy
 * 2023年4月11日09:15:08
 */
public class HttpClientUtil {
 
	/**
	 * get请求
	 * @param url 访问地址
	 * @param param 参数
	 * @param headerValues 请求头部参数
	 * @return
	 */
	public static String doGet(String url, Map<String, Object> param,Map<String,Object> headerValues) {
		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key)+"");
				}
			}
			URI uri = builder.build();
 
			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);
			if(null!=headerValues){
				for ( String key : headerValues.keySet() ) {
					httpGet.setHeader( key, headerValues.get( key ).toString() );
				}
			}
			// 执行请求
			response = httpclient.execute(httpGet);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}
 
	/**
	 * get请求
	 * @param url 请求地址
	 * @param headerValues 请求头部
	 * @return
	 */
	public static String doGet(String url,Map<String,Object> headerValues) {
		return doGet(url, null,headerValues);
	}
 
	/**
	 * post请求
	 * @param url
	 * @param param
	 * @return
	 */
	public static String doPost(String url, Map<String, Object> param,Map<String,Object> headerValues) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			if(null!=headerValues){
				for ( String key : headerValues.keySet() ) {
					httpPost.setHeader( key, headerValues.get( key ).toString() );
				}
			}
			// 创建参数列表
			if (param != null) {
				List<NameValuePair> paramList = new ArrayList<NameValuePair>();
				for (String key : param.keySet()) {
					paramList.add(new BasicNameValuePair(key, param.get(key)+""));
				}
				// 模拟表单
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
				httpPost.setEntity(entity);
			}
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
 
		return resultString;
	}
 
	/**
	 * post请求
	 * @param url
	 * @return
	 */
	public static String doPost(String url,Map<String,Object> headerValues) {
		return doPost(url, null,headerValues);
	}
	
	/**
	 * post请求json方式 
	 * @param url
	 * @param json 格式： String json="{\"name\":\"1发的\",\"text\":\"111\"}";
	 * @return
	 */
	public static String doPostJson(String url, String json,Map<String,Object> headerValues) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			if(null!=headerValues){
				for ( String key : headerValues.keySet() ) {
					httpPost.setHeader( key, headerValues.get( key ).toString() );
				}
			}
			// 创建请求内容
			StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPost.setEntity(entity);
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
 
		return resultString;
	}
	
	/**
	 * put请求
	 * @param url
	 * @param param
	 * @return
	 */
	public static String doPut(String url, Map<String, String> param,Map<String,Object> headerValues) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建HttpPut请求
			HttpPut httpPut = new HttpPut(url);
			if(null!=headerValues){
				for ( String key : headerValues.keySet() ) {
					httpPut.setHeader( key, headerValues.get( key ).toString() );
				}
			}
			// 创建参数列表
			if (param != null) {
				List<NameValuePair> paramList = new ArrayList<NameValuePair>();
				for (String key : param.keySet()) {
					paramList.add(new BasicNameValuePair(key, param.get(key)));
				}
				// 模拟表单
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
				httpPut.setEntity(entity);
			}
			// 执行http请求
			response = httpClient.execute(httpPut);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
 
		return resultString;
	}
	
	/**
	 * put请求
	 * @param url
	 * @param param
	 * @return
	 */
	public static String doPut(String url,Map<String,Object> headerValues) {
		return doPut(url, null,headerValues);
	}
	
	/**
	 * put请求
	 * @param url
	 * @param json 格式： String json="{\"name\":\"1发的\",\"text\":\"111\"}";
	 * @return
	 */
	public static String doPutJson(String url, String json,Map<String,Object> headerValues) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Put请求
			HttpPut httpPut = new HttpPut(url);
			if(null!=headerValues){
				for ( String key : headerValues.keySet() ) {
					httpPut.setHeader( key, headerValues.get( key ).toString() );
				}
			}
			// 创建请求内容
			StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPut.setEntity(entity);
			// 执行http请求
			response = httpClient.execute(httpPut);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}
	
	/**
	 * delete请求
	 * @param url  请求地址
	 * @param headerValues  请求头部
	 * @return
	 */
	public static String doDelete(String url,Map<String,Object> headerValues) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建HttpDelete请求
			HttpDelete httpDelete = new HttpDelete(url);
			if(null!=headerValues){
				for ( String key : headerValues.keySet() ) {
					httpDelete.setHeader( key, headerValues.get( key ).toString() );
				}
			}
			// 执行http请求
			response = httpClient.execute(httpDelete);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

	/**
	 * delete请求
	 * @param url  请求地址
	 * @param headerValues  请求头部
	 * @return
	 */
	public static String doDelete(String url,String json,Map<String,Object> headerValues) {
		// 创建默认的httpClient实例.
		CloseableHttpClient httpClient = HttpClients.createDefault();

		//以delte方式请求
		HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);

		// 设置请求头
		httpDelete.setHeader("Content-Type", "application/json;charset=UTF-8");
		httpDelete.setHeader("accept","application/json");
		if(null!=headerValues){
			for ( String key : headerValues.keySet() ) {
				httpDelete.setHeader( key, headerValues.get( key ).toString() );
			}
		}
		//将参数以UTF-8编码并包装成表单实体对象
		StringEntity se = new StringEntity(json, "UTF-8");
		se.setContentType("text/json");
		httpDelete.setEntity(se);
		// 执行请求
		CloseableHttpResponse response = null;
		String resultString=null;
		try {
			response = httpClient.execute(httpDelete);
			// 获取返回值
			HttpEntity entity = response.getEntity();
			 resultString = EntityUtils.toString(entity, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resultString;
	}
	
	
	
}
